package com.wangcang.widgetlibrary.share;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.bubble.PopUpUtils;
import com.wangcang.widgetlibrary.common.Common;
import com.wangcang.widgetlibrary.popupwindow.EasyPopupWindow;
import com.wangcang.widgetlibrary.tools.ToastUtils;

import java.util.LinkedList;
import java.util.List;


/**
 * author ：minifly
 * date: 2017/8/18
 * time: 09:49
 * desc: 分享弹出的popuwindow 里面做内容完整性及显示类型判断
 */
public class SharePopuwindow implements EasyPopupWindow.ChildClickListener, View.OnClickListener {
    public enum ShareType {
        WECHAT,WECHAR_CIRCLE,QQ,QQ_ZONE,MESSAGE,ERCODE,COPY,COMMIT
    }


    private static SharePopuwindow popuwindow = null;
    private Context mContext;
    private ShareContent content;
    private Dialog mDialog;

    public static SharePopuwindow getInstance(Context context) {
//        if(popuwindow == null){
        popuwindow = new SharePopuwindow(context);//每次的context需要更新，所以不能单例的来了。
//        }
        return popuwindow;
    }

    public SharePopuwindow(Context context) {
        this.mContext = context;
    }

    @Override
    public final void getChildView(int layoutId, View view) {
        LineGridView gridView = (LineGridView) view.findViewById(R.id.bottom_gridview_id);

        List<ShareType> list = new LinkedList<>();
        list.add(ShareType.MESSAGE);
        list.add(ShareType.ERCODE);
        list.add(ShareType.COMMIT);
        list.add(ShareType.COPY);
        list.add(ShareType.WECHAR_CIRCLE);
        list.add(ShareType.WECHAT);
        list.add(ShareType.QQ);
        list.add(ShareType.QQ_ZONE);


        gridView.setAdapter(new GridViewAdapter(list,mContext));
        gridView.setNumColumns(4);//设置gridview的列数
    }

    @Override
    public final void onClick(View v) {
        if (content == null) {
            ToastUtils.showToast("请设置分享内容！");
            return;
        }
    }

    public SharePopuwindow setContent(ShareContent content) {
        this.content = content;
        return popuwindow;
    }

    /**
     * 带取消确定的dialog
     *
     * @param title         标题
     * @param content       提示内容
     */
    private final static int TYPE_IMAGE = 10001, TYPE_TEXT = 10002, TYPE_ALL = 10003, TYPE_ALLNOTITLE = 10004;

    public void showCustomDialogPrompt(Context context, int type) {

        Button btnCancel = null, btnConfirm = null;
        View view = null;
        switch (type) {
            case TYPE_ALL://所有数据都有
                view = View.inflate(context, R.layout.share_bottom_dialog_alltype, null);

                ImageView mShareAlltypeHeadimageId = (ImageView) view.findViewById(R.id.share_alltype_headimage_id);
                ImageView shareAllImage = (ImageView) view.findViewById(R.id.share_alltype_image_id);
                TextView mShareAlltypeHeadnameId = (TextView) view.findViewById(R.id.share_alltype_headname_id);
                TextView title = (TextView) view.findViewById(R.id.share_alltype_title_id);
                TextView contentTxt = (TextView) view.findViewById(R.id.share_alltype_content_id);
                btnCancel = (Button) view.findViewById(R.id.share_alltype_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_alltype_btn_confirm);

                Glide.with(mContext)
                        .load(content.getImageUrl())
                        .placeholder(R.drawable.picture_no)
                        .into(shareAllImage);
                //目的地头像
                Glide.with(mContext)
                        .load(content.getShareDestImageurl())
                        .placeholder(R.drawable.picture_no)
                        .into(mShareAlltypeHeadimageId);

                title.setText(content.getTitle());
                contentTxt.setText(content.getContent());
                mShareAlltypeHeadnameId.setText(content.getShareDestname());


                break;
            case TYPE_ALLNOTITLE: //无title类型
                view = View.inflate(context, R.layout.share_bottom_dialog_alltypenotitle, null);
                ImageView mShareAlltypenotitleHeadimageId = (ImageView) view.findViewById(R.id.share_alltypenotitle_headimage_id);
                TextView mShareAlltypenotitleHeadnameId = (TextView) view.findViewById(R.id.share_alltypenotitle_headname_id);
                ImageView mShareAlltypenotitleImageId = (ImageView) view.findViewById(R.id.share_alltypenotitle_image_id);
                TextView mShareAlltypenotitleContentId = (TextView) view.findViewById(R.id.share_alltypenotitle_content_id);
                btnCancel = (Button) view.findViewById(R.id.share_alltypenotitle_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_alltypenotitle_btn_confirm);

                Glide.with(mContext)
                        .load(content.getImageUrl())
                        .placeholder(R.drawable.picture_no)
                        .into(mShareAlltypenotitleImageId);
                //目的地头像
                Glide.with(mContext)
                        .load(content.getShareDestImageurl())
                        .placeholder(R.drawable.picture_no)
                        .into(mShareAlltypenotitleHeadimageId);
                mShareAlltypenotitleHeadnameId.setText(content.getShareDestname());
                mShareAlltypenotitleContentId.setText(content.getContent());

                break;
            case TYPE_IMAGE: //只有图片
                view = View.inflate(context, R.layout.share_bottom_dialog_image, null);

                ImageView mShareImageHeadimageId = (ImageView) view.findViewById(R.id.share_image_headimage_id);
                TextView mShareImageHeadnameId = (TextView) view.findViewById(R.id.share_image_headname_id);
                ImageView mShareImageImageId = (ImageView) view.findViewById(R.id.share_image_image_id);
                btnCancel = (Button) view.findViewById(R.id.share_image_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_image_btn_confirm);

                Glide.with(mContext)
                        .load(content.getImageUrl())
                        .placeholder(R.drawable.picture_no)
                        .into(mShareImageImageId);
                //目的地头像
                Glide.with(mContext)
                        .load(content.getShareDestImageurl())
                        .placeholder(R.drawable.picture_no)
                        .into(mShareImageHeadimageId);
                mShareImageHeadnameId.setText(content.getShareDestname());

                break;
            case TYPE_TEXT: //只有文字+链接
                view = View.inflate(context, R.layout.share_bottom_dialog_text, null);

                ImageView mShareTextHeadimageId = (ImageView) view.findViewById(R.id.share_text_headimage_id);
                TextView mShareTextHeadnameId = (TextView) view.findViewById(R.id.share_text_headname_id);
                TextView mShareTextTextId = (TextView) view.findViewById(R.id.share_text_text_id);

                btnCancel = (Button) view.findViewById(R.id.share_text_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_text_btn_confirm);
                //目的地头像
                Glide.with(mContext)
                        .load(content.getShareDestImageurl())
                        .placeholder(R.drawable.picture_no)
                        .into(mShareTextHeadimageId);

                mShareTextHeadnameId.setText(content.getShareDestname());
                mShareTextTextId.setText(content.getContent());

                break;
        }

        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(false);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消掉
                mDialog.dismiss();
            }
        });
    }

    public void showPop() {
        PopUpUtils.showPopUpBottom(R.layout.share_bottom_layout, mContext, ((Activity) mContext).findViewById(android.R.id.content), true, this);
    }

    /**
     * 打电话
     *
     * @param phoneNumber
     */
    private void callPhone(String phoneNumber) {
        Uri uri = Uri.parse("tel:" + phoneNumber);
        Intent intent = new Intent(Intent.ACTION_CALL, uri);
        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
//            ToastUtils.showToast("拨打电话问题");
        }
    }

    /**
     * 发信息
     *
     * @param phoneNum
     * @param message
     */
    private void sendMessage(String phoneNum, String message) {
//        1、创建Uri，设置行为和号码
        Uri uri2 = Uri.parse("smsto:" + phoneNum);
//        2、创建意图。
        Intent intentMessage = new Intent(Intent.ACTION_VIEW, uri2);
//        3、打开系统短信界面，号码已经填写，只需填写要发送
        mContext.startActivity(intentMessage);
    }

    /**
     * 拷贝内容
     *
     * @param content
     */
    private void copyContent(String content) {
        // 从API11开始android推荐使用android.content.ClipboardManager
        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        try {
            cm.setPrimaryClip(ClipData.newPlainText(null, content));//报过时的方法下面的推荐的拷贝方法
            ToastUtils.showToast("拷贝到了剪切板");
        } catch (Exception e) {
//            ToastUtils.showToast("出现点问题");
        }
    }


    class GridViewAdapter extends BaseAdapter{
        List<ShareType> list ;
        Context mContext;

        public GridViewAdapter(List<ShareType> list,Context mcontext){
            this.list = list;
            this.mContext = mcontext;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GridViewHoder holder = null;

            if (convertView == null) {
                holder = new GridViewHoder(convertView);

                switch (list.get(position)){
                    case COMMIT://share_item_qq
                        convertView = View.inflate(mContext, R.layout.share_item_commit, null);
                        holder.lin = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.COMMIT;
                        break;
                    case WECHAR_CIRCLE:
                        convertView = View.inflate(mContext, R.layout.share_item_wechat_circle, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.WECHAR_CIRCLE;
                        break;
                    case WECHAT:
                        convertView = View.inflate(mContext, R.layout.share_item_wechat, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.WECHAT;
                        break;
                    case ERCODE:
                        convertView = View.inflate(mContext, R.layout.share_item_ercode, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.ERCODE;

                        break;
                    case QQ:
                        convertView = View.inflate(mContext, R.layout.share_item_qq, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.QQ;
                        break;
                    case QQ_ZONE:
                        convertView = View.inflate(mContext, R.layout.share_item_qqzone, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.QQ_ZONE;
                        break;
                    case MESSAGE:
                        convertView = View.inflate(mContext, R.layout.share_item_message, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.MESSAGE;
                        break;
                    case COPY:
                        convertView = View.inflate(mContext, R.layout.share_item_copy, null);
                        holder.lin  = (LinearLayout) convertView.findViewById(R.id.share_lin);
                        holder.currentType = ShareType.COPY;
                        break;


                }

                convertView.setTag(holder);
            } else {
                holder = (GridViewHoder) convertView.getTag();
            }


            final GridViewHoder finalHolder = holder;
            holder.lin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setOnlicklistener(finalHolder.currentType);
                }
            });

            return convertView;
        }

        class GridViewHoder{
            LinearLayout lin;
            ShareType currentType;

            public GridViewHoder(View view){
            }
        }

        public void setOnlicklistener(ShareType shareType){
            switch (shareType) {
                case COMMIT://share_item_qq
                    showCustomDialogPrompt(mContext, TYPE_TEXT);
                    break;
                case WECHAR_CIRCLE:
                    Common.getInstance().shareWeixinMoment(mContext, "" + content.getTitle(), content.getContent(), content.getImageUrl(), content.getUrl());
                    break;
                case WECHAT:
                    Common.getInstance().shareWeixinFriend(mContext, "" + content.getTitle(), content.getContent(), content.getImageUrl(), content.getUrl());
                    break;
                case ERCODE:

                    break;
                case QQ:
                    Common.getInstance().shareQQ(mContext, "" + content.getTitle(), content.getContent(), content.getImageUrl(), content.getUrl());
                    break;
                case QQ_ZONE:
                    Common.getInstance().shareQQZone(mContext, "" + content.getTitle(), content.getContent(), content.getImageUrl(), content.getUrl());
                    break;
                case MESSAGE:
                    sendMessage(content.getPhoneNum(), content.getMessage());
                    break;
                case COPY:
                    copyContent(content.getCopyContent());
                    break;
            }
        }

    }

}
